package myself.study.network.udp.demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
* @Name: UdpServer
* @Description: udp数据报文接收端
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 上午9:26:15 
* @Version: V1.0
 */
public class UdpServer {
	
	public static void main(String[] args) throws IOException {
		//1、创建接收端Socket对象
		DatagramSocket server = new DatagramSocket(12345) ;
		//2、创建接收数据包(udp数据包接收容器)
		byte[] buf = new byte[1024] ;
		DatagramPacket packet = new DatagramPacket(buf, buf.length) ;
		//3、接收客户端发送过来的数据
		server.receive(packet) ;
		//4、解析数据
		String s = new String(packet.getData(), 0, packet.getLength()) ;
		//获取客户端ip地址
		String hostAddress = packet.getAddress().getHostAddress() ;
		System.out.println("from [" + hostAddress + "] data is : " + s);
		//5、释放资源
		server.close() ;
	}

}
